##########################################

  #Name: build.sh
  #Desc: build php7 fpm base docker image
  #Author OracleGao
  #Email: oraclegao@126.com
  #Date 2019-12-06
  #Git: https://gitee.com/oraclegao/docker-image
  #Origin:

##########################################

#!/usr/bin/env bash
set -e
current_path=$(pwd)
cd $(dirname $0)
build_path=$(pwd)
cd ${current_path}

exts_path=${build_path}/exts

function empty_error() {
  echo "ERROR:"
  echo "  $1 parameter can not be empty"
  echo ""
  echo "Usage:"
  echo "  $0 -h | --help"
  echo ""
  exit 2
}

function usage_exts() {
  echo "AVAILABLE EXTS:"
  for ext in $(ls ${exts_path}); do
    printf "  %-26s %s module\n" ${ext}, ${ext}
  done
}

image_name=php7-fpm-base
image_tag=latest
function usage() {
  echo "build php7 fpm base image"
  echo ""
  echo "Usage:"
  echo "  $0 [OPTIONS] [EXTS...]"
  echo "  $0 -h | --help"
  echo "  $0 --empty-exts"
  echo "  $0 -O | --output b | c"
  echo ""
  echo "OPTIONS:"
  echo "  -a | --all                       setup all exts"
  echo "  -D | --debug                     debug mode, show debug message"
  echo "  -e | --exts                      show available ext modes and exit"
  echo "  -h | --help                      display this help and exit"
  echo "  -n | --name <string>             docker image name, (default: ${image_name})"
  echo "  -O | --output <b | c>            just only output dockerfile to console"
  echo "                                     b: base dockerfile"
  echo "                                     c: composer dockerfile"
  echo "                                     save dockerfile: $0 -O b | c > Dockerfile"
  echo "  -t | --tag <string>              docker image tag, (default: ${image_tag})"
  echo "  --empty-exts                     exts can be empty, allow setup none ext"
  echo "  --keep-exts-source               do not delete php exts source and scripts"
  echo "  --without-composer               do not build composer image"
  echo ""
  usage_exts
  exit 1
}

all_flag=0
debug_flag=0
without_composer_flag=0
empty_exts_flag=0
keep_exts_source_flag=0
args=()
for arg in "$@"; do
  if [ "$1" == "" ]; then
    break
  fi
  case $1 in
    -a|--all)
      all_flag=1
      shift
      ;;
    -D|--debug)
      debug_flag=1
      shift
      ;;
    -e|--exts)
      usage_exts
      exit 0
      ;;
    -h|--help)
      echo "init java app docker image and service build settings"
      echo ""
      usage
      ;;
    -n|--name)
      if [ $# -lt 2 ]; then
        empty_error $1
      fi
      image_name=$2
      shift
      shift
      ;;
    -O|--output)
      if [ $# -lt 2 ]; then
        empty_error $1
      fi
      output_flag=$2
      shift
      shift
      ;;
    -t|--tag)
      if [ $# -lt 2 ]; then
        empty_error $1
      fi
      image_tag=$2
      shift
      shift
      ;;
    --empty-exts)
      empty_exts_flag=1
      shift
      ;;
    --keep-exts-source)
      keep_exts_source_flag=1
      shift
      ;;
    --without-composer)
      without_composer_flag=1
      shift
      ;;
    *)
      args+=("$1")
      shift
      ;;
  esac
done

if [ "${debug_flag}" == "1" ]; then
  set -x
fi

if [ "${output_flag}" != "" ]; then
  if [ "${output_flag}" != "b" -a "${output_flag}" != "c" ]; then
    echo "Error:"
    echo "  invalid output parameter [${output_flag}]"
    echo "  $0 -O | --output <b | c>"
    echo ""
    usage
  fi
fi

exts=()
exts+=(${args[@]})
ext_count=${#exts[@]}

if [ "${empty_exts_flag}" == "0" -a ${ext_count} -eq 0 -a "${all_flag}" == "0" -a "${output_flag}" == "" ]; then
  usage
fi

for ext in ${exts[@]}; do
  flag=0
  for exist_ext in $(ls ${exts_path}); do
    if [ "${ext}" == "${exist_ext}" ]; then
      flag=1
      break
    fi
  done
  if [ "${flag}" == "0" ]; then
    echo "ERROR:"
    echo "  invalid ext name [${ext}]"
    echo ""
    usage_exts
    exit 3
  fi
done

tmp_path=${build_path}/.tmp
mkdir -p ${tmp_path}
dockerfile=${tmp_path}/Dockerfile.$(date "+%Y%m%d%H%M%S")

echo -e "FROM php:7.3-fpm"                                                          >> ${dockerfile}
echo -e "LABEL maintainer=\"oraclegao@126.com\""                                    >> ${dockerfile}
echo -e "WORKDIR /usr/src"                                                          >> ${dockerfile}
echo -e ""                                                                          >> ${dockerfile}
if [ "${all_flag}" == "1" -o ${ext_count} -gt 0 ]; then
  echo -e "RUN set -x \\"                                                             >> ${dockerfile}
  echo -e "  && apt-get update \\"                                                    >> ${dockerfile}
  echo -e "  && apt-get install --no-install-recommends --no-install-suggests -y \\"  >> ${dockerfile}
  echo -e "     wget \\"                                                              >> ${dockerfile}
  echo -e "  && docker-php-source extract"                                            >> ${dockerfile}
  echo -e ""                                                                          >> ${dockerfile}
  if [ "${all_flag}" == "1" ]; then
    for ext in $(ls ${exts_path}); do
      cat ${exts_path}/${ext} >> ${dockerfile}
      echo "" >> ${dockerfile}
    done
  else
    for ext in ${exts[@]}; do
      cat ${exts_path}/${ext} >> ${dockerfile}
      echo "" >> ${dockerfile}
    done
  fi
  echo -e "RUN set -x \\"                                                             >> ${dockerfile}
  echo -e "  && docker-php-source delete \\"                                          >> ${dockerfile}
  echo -e "  && apt-get remove --purge --auto-remove -y \\"                           >> ${dockerfile}
  echo -e "                    wget \\"                                               >> ${dockerfile}
  echo -e "  && apt-get autoremove -y \\"                                             >> ${dockerfile}
  echo -e "  && rm -rf /var/lib/apt/lists/* \\"                                       >> ${dockerfile}
  echo -e "  && rm -rf /etc/apt/sources.list.d/*"                                     >> ${dockerfile}
  echo -e ""                                                                          >> ${dockerfile}
fi
if [  "${keep_exts_source_flag}" == "0" ]; then
  echo -e "RUN set -x \\"                                                             >> ${dockerfile}
  echo -e "  && rm -rf /usr/local/bin/docker-php-ext-enable \\"                       >> ${dockerfile}
  echo -e "  && rm -rf /usr/local/bin/docker-php-source \\"                           >> ${dockerfile}
  echo -e "  && rm -rf /usr/local/bin/docker-php-ext-install \\"                      >> ${dockerfile}
  echo -e "  && rm -rf /usr/local/bin/docker-php-ext-configure \\"                    >> ${dockerfile}
  echo -e "  && rm -rf /usr/src/*"                                                    >> ${dockerfile}
  echo -e ""                                                                          >> ${dockerfile}
fi

  if [ "${output_flag}" == "" ]; then
    docker build --no-cache --compress -f ${dockerfile} -t ${image_name}:${image_tag} ${tmp_path}
  fi

if [ "${without_composer_flag}" == "0" ]; then
  dockerfile_composer=${tmp_path}/Dockerfile.Composer.$(date "+%Y%m%d%H%M%S")

  echo -e "FROM ${image_name}:${image_tag}"                                           >> ${dockerfile_composer}
  echo -e "LABEL maintainer=\"oraclegao@126.com\""                                    >> ${dockerfile_composer}
  echo -e ""                                                                          >> ${dockerfile_composer}
  echo -e "RUN set -x \\"                                                             >> ${dockerfile_composer}
  echo -e "  && apt-get update \\"                                                    >> ${dockerfile_composer}
  echo -e "  && apt-get install --no-install-recommends --no-install-suggests -y \\"  >> ${dockerfile_composer}
  echo -e "               git \\"                                                     >> ${dockerfile_composer}
  echo -e "               mercurial \\"                                               >> ${dockerfile_composer}
  echo -e "               openssh-client \\"                                          >> ${dockerfile_composer}
  echo -e "               subversion \\"                                              >> ${dockerfile_composer}
  echo -e "               tini \\"                                                    >> ${dockerfile_composer}
  echo -e "               unzip \\"                                                   >> ${dockerfile_composer}
  echo -e "               zip \\"                                                     >> ${dockerfile_composer}
  echo -e "  && rm -rf /var/lib/apt/lists/* \\"                                       >> ${dockerfile_composer}
  echo -e "  && rm -rf /etc/apt/sources.list.d/*"                                     >> ${dockerfile_composer}
  echo -e ""                                                                          >> ${dockerfile_composer}
  echo -e "COPY --from=composer /usr/bin/composer /usr/bin/composer"                  >> ${dockerfile_composer}
  echo -e "COPY --from=composer /docker-entrypoint.sh /docker-entrypoint.sh"          >> ${dockerfile_composer}
  echo -e ""                                                                          >> ${dockerfile_composer}
  echo -e "ENTRYPOINT [\"/bin/sh\", \"/docker-entrypoint.sh\"]"                       >> ${dockerfile_composer}
  echo -e "CMD [\"composer\"]"                                                        >> ${dockerfile_composer}

  if [ "${output_flag}" == "" ]; then
    docker build --no-cache --compress -f ${dockerfile_composer} -t ${image_name}-composer:${image_tag} ${tmp_path}
  fi
fi

if [ "${output_flag}" == "b" ]; then
  cat ${dockerfile}
elif [ "${output_flag}" == "c" ]; then
  cat ${dockerfile_composer}
fi

if [ "${debug_flag}" != "1" ]; then
  rm -rf ${tmp_path}
fi

